﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using RMS.Models.DatabaseManager;

namespace RMS.Pages
{
    public partial class CreateAccount : System.Web.UI.Page
    {
        private bool isAdmin = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["IsAdmin"] != null)
            {
                if ((bool)Session["IsAdmin"] == true)
                    isAdmin = true;
            }
        }

        protected void CreateUserButtonClick(object sender, EventArgs e)
        {
            //For some reason passing the Password.Text to the IsValidPassword would make the Text field lose
            //it's data. Saving the data to a variable fixes the issue. JM 10/20/13 
            string password = Password.Text;

            if(DidUserAgreeToTerms() && IsValidPassword(password) && DoPasswordsMatch(password, RepPassword.Text))
            {
                UserManager newUser = new UserManager();

                //Hash the password using SHA1 encryption.
                //password = FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text, "SHA1");
                bool userAdded = newUser.AddNewUser(FName.Text, LName.Text, MInt.Text, Address.Text, City.Text, State.SelectedValue, Zip.Text, Phone.Text, Email.Text, password);

                if (userAdded)
                {
                    if (isAdmin)
                    {
                        Response.Redirect("~/Pages/Admin/Users.aspx");
                    }
                    else
                    {
                        Session["ID"] = newUser.GetUserID();
                        Session["Name"] = FName.Text;
                        Session["Email"] = Email.Text;
                        Session["Password"] = password;
                        Response.Redirect("~/Pages/Home.aspx");
                    }
                }
            }
        }

        private bool DidUserAgreeToTerms()
        {
            AcceptTermsLabel.Text = "";

            if (TermsOfService.Checked)
                return true;
            else
            {
                AcceptTermsLabel.Text = "Please accept the terms before creating the account.";
                return false;
            }

            //return true;
                
        }

        private bool IsValidPassword(string pass)
        {
            Password.Text = "";

            //if (Password.Text.Length >= 7 && Password.Text.Length <= 10)
            if(pass.Length >= 7 && pass.Length <= 10)
                return true;
            else
            {
                InvalidPasswordLabel.Text = "* Please enter a valid password.";
            }
                return false;
        }

        private bool DoPasswordsMatch(string firstPassword, string secondPassword)
        {
            if (String.Compare(firstPassword, secondPassword) == 0)
                return true;
            else
            {
                PasswordWrong.Text = "* The passwords do not match";
                RepPassword.Text = "";
                return false;
            }
        }
    }
}